﻿/**
* File: TpmusermessageController.cs
* Author: 汪杰
* Create Time:2025/5/7 10:35:34
* Email:15200358008@139.com
* 代码由TMCodeGenerator自动生成
**/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using JSLCloud.App.Models.Tpmusermessage;
using JSLCloud.App.Mes.Service;
using JSLCloud.Models;
using JSLCloud.Framework;

namespace JSLCloud.App.Mes.Controllers
{
    /// <summary>
    /// t_pm_usermessage Controller
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class TpmusermessageController : ControllerBase
    {
        private readonly ITpmusermessageService _service;
        
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public TpmusermessageController(ITpmusermessageService service)
        {
            _service = service;
        }
        
        /// <summary>
        /// 获取t_pm_usermessage数据表数据
        /// </summary>
        /// <param name="requestObject">返回响应结果</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseObject<List<TpmusermessageQueryModel>>> Get(string requestObject)
        {
            var request = JsonConvert.DeserializeObject<RequestGet>(requestObject);
            return await _service.GetAsync(request);
        }
        
        /// <summary>
        /// 新增t_pm_usermessage数据表数据，支持批量新增
        /// </summary>
        /// <param name="requestObject">返回响应结果</param>
        [HttpPost]
        [HttpOptions]
        public async Task<ResponseObject<bool>> Post(RequestPost<TpmusermessageAddModel> requestObject)
        {
            var user = TokenManager.GetCurentUserbyToken(Request.Headers);
            if (requestObject.PostData != null)
            {
                requestObject.PostData.Createtime = DateTime.Now;
                requestObject.PostData.Createid = user.UserID;
                requestObject.PostData.Createname = user.UserName;
            }
            if (requestObject.PostDataList != null)
            {
                foreach (var item in requestObject.PostDataList)
                {
                    item.Createtime = DateTime.Now;
                    item.Createid = user.UserID;
                    item.Createname = user.UserName;
                }
            }
            return await _service.PostAsync(requestObject);
        }
        
        /// <summary>
        /// 修改t_pm_usermessage数据表数据，支持批量修改
        /// </summary>
        /// <param name="requestObject">返回响应结果</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResponseObject<bool>> Put(RequestPut<TpmusermessageEditModel> requestObject)
        {
            var user = TokenManager.GetCurentUserbyToken(Request.Headers);
            if (requestObject.PostData != null)
            {
                requestObject.PostData.Updatetime = DateTime.Now;
                requestObject.PostData.Updateid = user.UserID;
                requestObject.PostData.Updatename = user.UserName;
            }
            if (requestObject.PostDataList != null)
            {
                foreach (var item in requestObject.PostDataList)
                {
                    item.Updatetime = DateTime.Now;
                    item.Updateid = user.UserID;
                    item.Updatename = user.UserName;
                }
            }
            return await _service.PutAsync(requestObject);
        }
        
        /// <summary>
        /// 删除t_pm_usermessage数据表数据
        /// </summary>
        /// <param name="requestObject">返回响应结果</param>
        [HttpDelete]
        public async Task<ResponseObject<bool>> Delete(RequestDelete<DeleteModel> requestObject)
        {
            return await _service.DeleteAsync(requestObject);
        }
    }
}
